/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.uct.common  
 */
package com.apache.platform.web;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.cache.util.Validator;

/**
 * description:  统一跳jsp页面
 * @author dwusoft 创建时间：2015-11-3  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
@Controller
@RequestMapping("/sendPage")
public class SendPageAction {

	/**
	 * 用户直接跳转JSP页面，四层目录足够了吧，不够自己加<br/>
	 * @param folder路径 <br/>
	 * @param jspNameJSP名称(不加后缀) <br/>
	 * @return 指定JSP页面
	 */
	@RequestMapping("/{jspName}")
	public String redirectJsp(HttpServletRequest request, @PathVariable String jspName) {
		setToken(request);
		return "/" + jspName;
	}

	@RequestMapping("/{folder}/{jspName}")
	public String redirectJsp(HttpServletRequest request, @PathVariable String folder, @PathVariable String jspName) {
		setToken(request);
		return "/" + folder + "/" + jspName;
	}

	@RequestMapping("/{folder1}/{folder2}/{jspName}")
	public String redirectJsp(HttpServletRequest request, @PathVariable String folder1, @PathVariable String folder2,
			@PathVariable String jspName) {
		setToken(request);
		return "/" + folder1 + "/" + folder2 + "/" + jspName;
	}

	@RequestMapping("/{folder1}/{folder2}/{folder3}/{jspName}")
	public String redirectJsp(HttpServletRequest request, @PathVariable String folder1, @PathVariable String folder2,
			@PathVariable String folder3, @PathVariable String jspName) {
		setToken(request);
		return "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + jspName;
	}

	@RequestMapping("/{folder1}/{folder2}/{folder3}/{folder4}/{jspName}")
	public String redirectJsp(HttpServletRequest request, @PathVariable String folder1, @PathVariable String folder2,
			@PathVariable String folder3, @PathVariable String folder4, @PathVariable String jspName) {
		setToken(request);
		return "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + folder4 + "/" + jspName;
	}

	@RequestMapping("/token")
	@ResponseBody
	public String token(HttpServletRequest request) {
		String uuid = setToken(request);
		return uuid;
	}

	private String setToken(HttpServletRequest request) {
		if (Validator.isNotNull(request.getParameter("token"))) {
			HttpSession session = request.getSession(true);
			String uuid = UUID.randomUUID().toString();
			if (null != session)
				session.setAttribute("formToken", uuid);
			return uuid;
		}
		return "";
	}
}
